<#
    重命名zone流程
    > zoneCreate "zNewName", "21:00:00:24:ff:52:99:04; 20:12:f0:1f:af:d5:c3:9d; 20:23:f0:1f:af:d5:c3:9d;" 
    > cfgadd "cfgName", "zNewName"
    > cfgRemove "cfgName", "zOldName"
    > cfgenable "cfgName"
#>

function GetCfgName([string]$BrocadeCfgFile)
{
    [string]$RegEx_FcSwCfg="cfg.(\S+):";
    $LastLine="";
    $cfgName="";
    foreach($line in Get-Content $BrocadeCfgFile){
        if(($line -cmatch $RegEx_FcSwCfg) -and ($LastLine -eq "[Zoning]")){
            #Write-Host $Matches[1];        
            $cfgName = $Matches[1];
        }
        $LastLine=$line;
    }
    return $cfgName;
}

<#
  @brief  使用Brocade交换机导出的配置文件生成 zoneCreate 命令
  @Notes  None.
  @param  $BrocadeCfgFile, Brocade原始配置文件
  @param  $zNameOldPartRegEx, zone Name需要替换部分(旧)的正则式字符串，例如：8([kK])
  @param  $zNameNewPartRegEx, zone Name需要替换部分(新)的正则式字符串，例如：9`$1
  @retval None.
#>
function zoneRename([string]$BrocadeCfgFile, [string]$zNameOldPartRegEx, [string]$zNameNewPartRegEx)
{
    <################################# Edit regular expression ############################>
    [string]$RegEx_Match="zone.([^:]+):(\S+)";
    # $0: All  $1: (Seg1) $2: (Seg2)  $3: (Seg3)
    [string]$RegEx_Replace="(\S+)("+$zNameOldPartRegEx+")(\S*)"; 
    <######################################################################################>
    $cfgName = GetCfgName($BrocadeCfgFile);
    foreach($line in Get-Content $BrocadeCfgFile) {  
        if($line -cmatch $RegEx_Match){  # case sensitive RegEx match
            $zName=$Matches[1]; $zMember=$Matches[2];
            if($zName -match $RegEx_Replace){ # Non-case sensitive RegEx match
              #  if(($Matches[2] -eq "8k") -or ($Matches[2] -eq "8K")){
                    [string]$nzCreate="zoneCreate `"";
                    #字符串正则替换"运算"，不是调用String.Split方法
                    #$nzCreate+=$Matches[0] -Replace $zNameOldPartRegEx, $zNameNewPartRegEx; 
                    $nzName=$Matches[0] -Replace $zNameOldPartRegEx, $zNameNewPartRegEx; 
                    $nzCreate+=$nzName;
                    $nzCreate+="`", `"";
                    for($i = 0; $i -lt $zMember.Split(";").Count; $i++){
                        $nzCreate += $zMember.Split(";")[$i];
                        if($i -lt ($zMember.Split(";").Count-1)){
                            $nzCreate += "; ";
                        }
                    }
                    $nzCreate+="`""; 
                    $nzAdd="cfgAdd "+"`""+$cfgName+"`", `""+$nzName+"`"";
                    $ozDelete = "zoneDelete "+"`""+$zName+"`"";
                    $ozCfgRemove = "cfgRemove "+"`""+$cfgName+"`", `""+$zName+"`"";
                    Write-Host $nzCreate; 
                    $nzCreate | Out-File -Append "$PSScriptRoot\zCreate.txt" #写入文件
                    #Write-Host $ozDelete;
                    #$ozDelete | Out-File -Append "$PSScriptRoot\zDelete.txt" #写入文件
                    Write-Host $nzAdd;
                    $nzAdd | Out-File -Append "$PSScriptRoot\zCfgAdd.txt"
                    Write-Host $ozCfgRemove;
                    $ozCfgRemove | Out-File -Append "$PSScriptRoot\zCfgRemove.txt"
              #  }
            }
        }
    }
}

#字符$写入string中，需要转义符号修饰
#PWSH脚本与原始配置文件放同一路径下
zoneRename "$PSScriptRoot\6510_2.txt" "8([kK])" "9`$1"  